Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - [VB.NET 2010] Tastiera su schermo & FOCUS
Forum - C# / VB.NET - [VB.NET 2010] Tastiera su schermo & FOCUS

Avatar
gecko6989 (Normal User)
Pro


Messaggi: 64
Iscritto: 05/06/2007

Segnala al moderatore
Postato alle 23:17
Mercoledì, 13/10/2010
Ciao a tutti,
sto realizzando una tastiera su schermo identica a quella incorporata in windows....
Premetto che per fare ci sto usando il metodo sendkeys.

Il programma funge con una textbox perchè faccio:
Codice sorgente - presumibilmente VB.NET

  1. Private Sub Cmd01_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmd01.Click
  2.         TextBox1.Focus()
  3.  
  4.         My.Computer.Keyboard.SendKeys("A", True)
  5.     End Sub



Il problema sta nel fatto che quando voglio scrivere su un blocco note non ci riesco....perchè?



PM
Avatar
Il Totem (Admin)
Guru^2


Messaggi: 3635
Iscritto: 24/01/2006

Up
1
Down
V
Segnala al moderatore
Postato alle 13:31
Giovedì, 14/10/2010
La SendKeys serve proprio da wrapper per evitare di accedere all'api, quindi mi sembra strano che non funzioni. Se sei disperato, puoi provare la funzione keydb_event o la più recente SendInput definite nella user32.dll.

PM
Avatar
gecko6989 (Normal User)
Pro


Messaggi: 64
Iscritto: 05/06/2007

Up
0
Down
V
Segnala al moderatore
Postato alle 0:09
Giovedì, 14/10/2010
Ho risolto con un codice trovate sul web:


Codice sorgente - presumibilmente VB.NET

  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class Form1
  4.  
  5.     <DllImport("user32.dll", SetLastError:=True)> _
  6.     Private Shared Function GetForegroundWindow() As IntPtr
  7.     End Function
  8.     <DllImport("user32.dll")> _
  9.     Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
  10.     End Function
  11.  
  12.  
  13.     Public Sub New()
  14.         ' This call is required by the designer.
  15.         InitializeComponent()
  16.  
  17.         ' Add any initialization after the InitializeComponent() call.
  18.         Timer1.Interval = 100
  19.         Timer1.Start()
  20.     End Sub
  21.     Private window As IntPtr
  22.     Private Sub Timer1_Tick _
  23.         (ByVal sender As System.Object, ByVal e As System.EventArgs) _
  24.         Handles Timer1.Tick
  25.         Dim iptr As IntPtr = GetForegroundWindow()
  26.         If (iptr <> Me.Handle) Then window = iptr
  27.     End Sub
  28.  
  29.  
  30.     Private Sub Cmd01_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmd01.Click
  31.         SetForegroundWindow(window)
  32.         My.Computer.Keyboard.SendKeys("A")
  33.     End Sub
  34. End Class




Qualcuno è in grado di spiegarmi come funziona?
Sopratutto, è possibile semplificare questo codice?

PM